home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part2 / 13409 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  3.9 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: help with strcmp
  5. Date: Sat, 06 Apr 96 19:32:07 GMT
  6. Organization: none
  7. Message-ID: <828819127snz@genesis.demon.co.uk>
  8. References: <4jpiek$lp6@blaze.cs.jhu.edu> <DpAI6o.2Cq@iquest.net> <4jup7d$8s5@solutions.solon.com> <4jurqhINNao@anvil.ugrad.cs.ubc.ca>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4jurqhINNao@anvil.ugrad.cs.ubc.ca>
  15.            c2a192@ugrad.cs.ubc.ca "Kazimir Kylheku" writes:
  16.  
  17. >In article <4jup7d$8s5@solutions.solon.com>,
  18. >Peter Seebach <seebs@solutions.solon.com> wrote:
  19. >>Once again: feof() informs you only that the last read failed; it doesn't
  20. >>belong before a read.
  21. >
  22. >Whoa, are you implying that the feof() status is _undefined_ before you do the
  23. >first read or merely that it's an indication of bad style to have the check
  24. >before a read?
  25.  
  26. No, feof() is well defined before the first read. However the point is that
  27. feof() should be tested (if at all) after the corresponding operation, not
  28. before. That can be no operation if, for example, you don't know whether
  29. there has been any previous I/O performed on the stream.
  30.  
  31. >I recently posted some code which does check ferror() and feof()
  32. >before doing a read, although not in the same sense that Pascal newbies make
  33. >the mistake. It's the code in which lines from two files are read
  34. >concurrently (in the lame sense of the word, of course!), and as long as one of
  35. >them has no ferror() or feof() condition, the loop has to continue.  The outer
  36. >loop does a bottom test, but inside the loop, the entry into two scanning
  37. >inner-loops is guarded by checking ferror() and feof(), which the first time
  38. >around happens before anything is read.
  39.  
  40. The point is that here your feof() and ferror() tests do relate to prior 
  41. reads so even in your example they come after the corresponding read as far
  42. as the order of execution is concerned. You also perform a test after
  43. each read in the loop i.e. before the data is 'processed'. However now you
  44. bring it up I'll comment on the code!
  45.  
  46. >#include <stdio.h>
  47. >#include <stdlib.h>
  48. >#include <errno.h>
  49.  
  50. I don't see any need for stdlib.h or errno.h.
  51.  
  52. >void concatprint(FILE *f1, FILE *f2)
  53. >
  54. >/*
  55. > * read lines from f1 and f2 simultaneously, and print them side by side
  56. > * f1 and f2 must be valid streams open for reading.
  57. > */
  58. >
  59. >{
  60. >    int c;
  61. >
  62. >    do {
  63. >        if (!feof(f1) && !ferror(f1)) 
  64. >            while ((c = getc(f1)) != '\n' && c != EOF)
  65. >                putchar(c);
  66. >        if (!feof(f2) && !ferror(f2)) 
  67. >            while ((c = getc(f2)) != '\n' && c != EOF)
  68. >                putchar(c);
  69. >        putchar('\n');
  70. >    } while ((!feof(f1) && !ferror(f1)) || (!feof(f2) && !ferror(f2)));
  71. >}
  72.  
  73. If you hit an error on one of the reads it is questionable as to whether
  74. the operation as a whole should continue.
  75.  
  76. The feof() and ferror() tests in the loop check whether the I/O on a file
  77. has been completed but allows further reading from the other file. There
  78. are other ways to achieve the same effect e.g. you exit the main loop once
  79. either of the files has hit end-of-file and then copy the rest of the other
  80. file as a separate operation. You could also hold the completion indication
  81. separately e.g.
  82.  
  83. ...
  84.  
  85. {
  86.     int c1 = 0, c2 = 0;
  87.  
  88.     do {
  89.         if (c1 != EOF) {
  90.             while ((c1 = getc(f1)) != '\n' && c1 != EOF)
  91.                 putchar(c1);
  92.         }
  93.  
  94.         if (c2 != EOF) {
  95.             while ((c2 = getc(f1)) != '\n' && c2 != EOF)
  96.                 putchar(c2);
  97.         }
  98.  
  99.         putchar('\n');
  100.     } while (c1 != EOF || c2 != EOF);
  101. }
  102.  
  103. avoids the whole issue of feof() by simply not using it!
  104.  
  105. There is still the issue of whether to continue going on an error. Both of
  106. these functions should test putchar() for failure.
  107.  
  108. -- 
  109. -----------------------------------------
  110. Lawrence Kirby | fred@genesis.demon.co.uk
  111. Wilts, England | 70734.126@compuserve.com
  112. -----------------------------------------
  113.